﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;
using Reborn_WorldServer.App1AppHandler.Spells.Interfaces;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_Rabies : Buff, IInstantCast
    {
        public Spell_Rabies()
        {
            minutesLeft = 5;
        }

        public override bool tryResist(SpellIcon _spellIcon, int castLevel, int castInt, int castWis, CreatureBase targetCreatureBase)
        {
            int penetration = SkillManager.AdjustForCombat(castLevel, 0, castInt);

            int resist = 0;
            if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                resist = targetCreatureBase.getResist(_spellIcon.Skill);
            else
                resist = targetCreatureBase.getResist(_spellIcon.Skill) * 2;

            int result = Managers.GameHandler.random.Next(1, penetration + resist + 1);
            if (result > penetration)
                return true;

            return false;
        }

        //TODO: This is a creature only spell. Fix this to use the base class & also be player castable.
        public void InstantCast(CreatureBase castingCreature, CreatureBase targetCreatureBase, SpellIcon _spellIcon, int castLevel)
        {
            if (InitializeInstantCast(castingCreature, targetCreatureBase, _spellIcon, castLevel, true) == false)
                return;

            TargetCreatureBase.buffManager.UpdateBuffs();

            //foreach (MapItem mapitem in TargetCreatureBase.currentMapItem.mapItemsWithin4)
            //{
            //    if (Math.Abs(mapitem.MapX - targetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - targetCreatureBase.MapY) <= 4)
            //    {
            //        foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
            //        {
            //            if (creatureBase is Character)
            //            {
            //                Character charToUpdate = (Character)creatureBase;
            //                charToUpdate.AddCreatureBase(targetCreatureBase);
            //            }
            //        }
            //    }
            //}

            spellTimer.Start();
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);
            minutesLeft = timeRemaining;
            spellTimer.Start();
        }
    }
}
